import { useMemo } from 'react';
import { useHistory } from 'react-router-dom';
import USER from './constants';

/**
 * 判断路由是否在允许范围内
 */
export const useCheckUserInfoRange = (
  currentRouteIndex: number,
  routerKeysArrLength: number,
  questionnaireUuid: string
) => {
  const history = useHistory();
  // 显示题目的路由范围
  const isInQuestionRange = (currentRouteIndex >= 0)
    && (currentRouteIndex < routerKeysArrLength);
  // 超出路由范围
  if (isInQuestionRange === false) {
    history.push(`/questionAnswer/home/${questionnaireUuid}/closeResult`);
  }
};

/**
 * key数组
 * @param workUnitVisible 工作单位
 * @param affiliatedVisible 附属单位
 * @param jobPositionVisible 工作职位
 * @param operatingPostVisible 工作岗位
 * @returns {routerKeysArr: []}
 */
export const useRouterKeysArr = (
  userNameVisible: boolean,
  userNumVisible: boolean,
  workUnitVisible: boolean,
  affiliatedVisible: boolean,
  jobPositionVisible: boolean,
  operatingPostVisible: boolean
) => useMemo(
  () => {
    const routerKeysArr = [
      USER.EMAIL,
      USER.GENDER,
      USER.MARITAL_STATUS,
      USER.CHILDREN_SITUATION,
      USER.AGE,
      USER.EDUCATION,
    ];
    if (userNumVisible) {
      routerKeysArr.unshift(USER.NUM);
    }
    if (userNameVisible) {
      routerKeysArr.unshift(USER.NAME);
    }
    if (workUnitVisible) {
      routerKeysArr.push(USER.WORK_UNIT);
    }
    if (affiliatedVisible) {
      routerKeysArr.push(USER.AFFILIATED);
    }
    if (jobPositionVisible) {
      routerKeysArr.push(USER.JOB_POSITION);
    }
    if (operatingPostVisible) {
      routerKeysArr.push(USER.OPERATING_POST);
    }
    return routerKeysArr;
  },
  []
);
